//
//  opinionViewController.m
//  ioschishan
//
//  Created by 千翼网络科技 on 2017/7/15.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "opinionViewController.h"
#import "IQKeyboardManager.h"

@interface opinionViewController ()

@property (nonatomic,strong) UITextView *messageTextView;
@property (nonatomic,strong) UITextField *phoneTextField;

@end

@implementation opinionViewController

- (void)viewWillDisappear:(BOOL)animated{
    
    [IQKeyboardManager sharedManager].enable = YES;
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initView];
}

#pragma mark ------------------ init -------------------

- (void)initView{
    
    [IQKeyboardManager sharedManager].enable = NO;
    self.title = @"意见与反馈";
    
    UIView *threadView = [[UIView alloc]initWithFrame:CGRectMake(0, screenScale(1), scrWidth(), screenScale(0.7))];
    threadView.backgroundColor = getColor(@"cccccc");
    [self.view addSubview:threadView];
    
    _messageTextView = [[UITextView alloc]init];
    _messageTextView.font = get4sFont(12);
    _messageTextView.backgroundColor = getColor(@"ffffff");
    _messageTextView.textColor = getColor(@"000000");
    [self.view addSubview:_messageTextView];
    [_messageTextView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(15));
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(200));
    }];
    
    UILabel *placeHolderLabel = [[UILabel alloc] init];
    placeHolderLabel.text = @"亲，我们非常重视您给我们提出的宝贵意见，帮助我们不断完善产品，谢谢！";
    placeHolderLabel.numberOfLines = 0;
    placeHolderLabel.font = get4sFont(12);
    
    placeHolderLabel.textColor = getColor(@"9b9b9b");
    [placeHolderLabel sizeToFit];
    [_messageTextView addSubview:placeHolderLabel];
    [_messageTextView setValue:placeHolderLabel forKey:@"_placeholderLabel"];
    
    ///
    
    UIView *thread = [[UIView alloc]initWithFrame:CGRectMake(screenScale(15), screenScale(200), scrWidth() - screenScale(15), screenScale(0.5))];
    thread.backgroundColor = getColor(@"eeeeee");
    [self.view addSubview:thread];
    
    _phoneTextField = [[UITextField alloc]init];
    _phoneTextField.placeholder = @"请留下您的手机或邮箱";
    _phoneTextField.returnKeyType = UIReturnKeyGo;
    _phoneTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _phoneTextField.font = get4sFont(12);
    [self.view addSubview:_phoneTextField];
    [_phoneTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(thread).with.offset(0);
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(44));
    }];
    
    UIView *thread2 = [[UIView alloc]init];
    thread2.backgroundColor = getColor(@"eeeeee");
    [self.view addSubview:thread2];
    [thread2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_phoneTextField.mas_bottom).with.offset(0);
        make.left.mas_equalTo(self.view).with.offset(screenScale(15));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-15));
        make.height.mas_equalTo(screenScale(0.5));
    }];
    
    ///
    
    UIButton *finishBtn = [ConfigXp buttonWithFrame:CGRectMake(screenScale(30), screenScale(154), screenScale(315), screenScale(44))
                                              title:@"提交"
                                         titleColor:getColor(@"ffffff")
                                               font:get4sFont(13)
                                    backgroundColor:getColor(@"3a95ff")
                                              image:nil];
    [finishBtn addTarget:self action:@selector(finishBtnClick) forControlEvents:UIControlEventTouchUpInside];
    finishBtn.layer.cornerRadius = screenScale(3);
    [self.view addSubview:finishBtn];
    [finishBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(thread2.mas_bottom).with.offset(25);
        make.left.mas_equalTo(self.view).with.offset(screenScale(30));
        make.right.mas_equalTo(self.view).with.offset(screenScale(-30));
        make.height.mas_equalTo(screenScale(44));
    }];
}

#pragma mark ------------------ click -------------------

- (void)finishBtnClick{
    
    if (_messageTextView.text.length < 1) {
        
        [PublicFunction showAlert:nil message:@"请填写内容" duration:1];
        return;
        
    }
    
    if (![TSRegularExpressionUtil validateMobile:_phoneTextField.text] && ![TSRegularExpressionUtil validateEmail:_phoneTextField.text]) {
        
        [PublicFunction showAlert:nil message:@"请输入正确格式的手机号码或邮箱" duration:1.0];
        return;
    }
    
    [self.view endEditing:YES];
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: [PublicFunction getConfigWithKey:@"token"] forKey:@"token"];
    [productParams setValue: _messageTextView.text forKey:@"content"];
    [productParams setValue: _phoneTextField.text forKey:@"contact"];
    
    [[MNetworkHandler sharedInstance]conURL:more_feedbackAdd
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
//                                       [PublicFunction showAlert:@"1" Mesasge:[returnData objectForKey:@"msg"] viewController:self completion:^(BOOL finished) {
//                                           if (finished) {
//                                               
//                                               NSLog(@"1");
//                                               [self.navigationController popViewControllerAnimated:YES];
//                                           }
//                                           
//                                       }];
//                                       
//                                       NSLog(@"1");
                                       [self.navigationController popViewControllerAnimated:YES];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                   }
                                   
                               } failureBlock:^(NSError *error) {
                                   
                                   [hud removeFromSuperview];
                                   NSLog(@"%@",error);
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

@end
